package  
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * Create a bitmapdata and stores the info about the getBounds.
	 * @author Lorenzo Nuvoletta
	 */
	public class CustomBMPD extends BitmapData
	{		
		public var mcRect:Rectangle;		
		
		public function CustomBMPD(mb:MovieBMPD, normal:Boolean = false ) 
		{			
			var _spr:Sprite = new Sprite();
			_spr.addChild(mb.mc);
			
			mb.mc.gotoAndStop(mb.currentFrame);
			
			if(!normal){
				mb.mc.scaleX = mb.scaleX;
				mb.mc.scaleY = mb.scaleY;
				mb.mc.rotation = mb.rotation;
			}
			
			mcRect = mb.mc.getBounds(_spr);
			
			_spr = null;
			
			var tempBMPD:BitmapData = new BitmapData(mcRect.width, mcRect.height, true, 0);
			
			var matrix:Matrix = new Matrix();			
			if(!normal){
				matrix.scale( mb.scaleX, mb.scaleY);
				matrix.rotate(mb.rotation * Math.PI / 180);
			}						
			matrix.translate( -mcRect.x, -mcRect.y);
			
			tempBMPD.draw(mb.mc, matrix, null, null, null, true);
			tempBMPD = trim(tempBMPD);
			super(tempBMPD.width, tempBMPD.height, true, 0);
			copyPixels(tempBMPD, tempBMPD.rect, new Point());
		}
		
		private function trim(bmp:BitmapData):BitmapData
		{
			var trect:Rectangle = bmp.getColorBoundsRect(0xff000000, 0, false);
			if (trect.isEmpty()) return bmp;
			var temp:BitmapData = new BitmapData(trect.width, trect.height, true, 0x00000000);
			mcRect.x += trect.x;
			mcRect.y += trect.y;
			temp.copyPixels(bmp, trect, new Point(0, 0));		
			return temp;
		}
		
	}

}